package com.miniapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miniapp.constant.status.MiniNormalStatus;
import com.miniapp.entity.*;
import com.miniapp.mapper.*;
import com.miniapp.service.MiniUserTemplateInfoService;
import com.miniapp.utils.R;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import  com.miniapp.utils.BaseUtils;

import org.springframework.beans.BeanUtils;

import com.miniapp.service.MiniUserTemplateService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service("miniUserTemplateService")
public class MiniUserTemplateServiceImpl extends BaseServiceImpl<MiniUserTemplateMapper,MiniUserTemplateEntity> implements MiniUserTemplateService {

    @Resource
    private MiniTemplateMapper miniTemplateMapper;
    @Resource
    private MiniUserTemplateInfoService miniUserTemplateInfoService;
    @Resource
    private MiniTemplateInfoMapper miniTemplateInfoMapper;
    @Resource
    private MiniUserTemplateInfoMapper miniUserTemplateInfoMapper;

    @Override
    public List<MiniUserTemplateEntity> findByUserId(Long userId) {

        return baseMapper.selectList(new QueryWrapper<MiniUserTemplateEntity>().eq("user_id",userId).orderByDesc("used"));
    }

    @Override
    @Transactional
    public R create(Long id, String appid) {

        MiniTemplateEntity miniTemplate = miniTemplateMapper.selectById(id);

        MiniUserTemplateEntity miniUserTemplate = new MiniUserTemplateEntity();

        BeanUtils.copyProperties(miniTemplate, miniUserTemplate, new String[] { "id", "createTime", "updateTime","optlock" });

        miniUserTemplate.setAppid(appid);
        /**
         * 判断是否存在用户模版。没有则默认使用
         */
        if (BaseUtils.isEmpty(this.findByUsedAndAppid(appid))){
            miniUserTemplate.setUsed(MiniNormalStatus.YES.getKey());
        }else {
            miniUserTemplate.setUsed(MiniNormalStatus.NO.getKey());
        }
        baseMapper.insert(miniUserTemplate);

        List<MiniTemplateInfoEntity> miniTemplateInfos = miniTemplateInfoMapper.selectList(new QueryWrapper<MiniTemplateInfoEntity>().eq("tmp_id",id));

        for (MiniTemplateInfoEntity miniTemplateInfo:miniTemplateInfos){
            MiniUserTemplateInfoEntity entity = new MiniUserTemplateInfoEntity();
            BeanUtils.copyProperties(miniTemplateInfo, entity, new String[] { "id", "createTime", "updateTime","optlock" });

            entity.setUserTmpId(miniUserTemplate.getId());
            entity.setHome(miniTemplateInfo.getHome());
            miniUserTemplateInfoMapper.insert(entity);
        }
        return R.ok().put("id",miniUserTemplate.getId());
    }

    @Override
    public MiniUserTemplateEntity findByUsedAndAppid(String appid) {
        return baseMapper.selectOne(new QueryWrapper<MiniUserTemplateEntity>().eq("appid",appid).eq("used",1));
    }

    @Override
    @Transactional
    public R saveAsTmp(MiniUserTemplateEntity dto) {

        try {
            MiniUserTemplateEntity miniUserTemplate = baseMapper.selectById(dto.getId());
            MiniUserTemplateEntity entity = new MiniUserTemplateEntity();
            BeanUtils.copyProperties(miniUserTemplate, entity, new String[] { "id", "createTime", "updateTime","optlock" });
            entity.setName(dto.getName());
            entity.setImagePath(dto.getImagePath());
            entity.setUsed("0");
            baseMapper.insert(entity);

            List<MiniUserTemplateInfoEntity> miniUserTemplateInfos = miniUserTemplateInfoService.findByUserTmpId(dto.getId());
            for (MiniUserTemplateInfoEntity miniUserTemplateInfo:miniUserTemplateInfos){
                MiniUserTemplateInfoEntity entity1 = new MiniUserTemplateInfoEntity();
                BeanUtils.copyProperties(miniUserTemplateInfo, entity1, new String[] { "id", "createTime", "updateTime","optlock" });
                entity1.setUserTmpId(entity.getId());
                miniUserTemplateInfoMapper.insert(entity1);
            }
        } catch (BeansException e) {
            return R.error("设置模版失败");
        }
        return R.ok();
    }

    @Override
    public List<MiniUserTemplateEntity> findByAppid(String appid) {
        return baseMapper.selectList(new QueryWrapper<MiniUserTemplateEntity>().eq("appid",appid));
    }

    @Override
    @Transactional
    public R use(Long id, String appid) {

        try {
            baseMapper.updateUse(appid);

            MiniUserTemplateEntity miniUserTemplate = baseMapper.selectById(id);

            miniUserTemplate.setUsed(MiniNormalStatus.YES.getKey());

            baseMapper.updateById(miniUserTemplate);
        }catch (Exception e){
            return R.error("表更新失败");
        }

        return R.ok();
    }
}
